package com.superman.supermarket.client.composite.preorder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.utils.StringUtils;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.composite.preorder.sku.PreorderSkuModule;

/**
 * 提交订单页面店铺模块信息
 * 
 * @author superman
 * @version 2018年8月6日下午10:28:12
 */
public class PreorderStoreModule extends Composite implements IUIModule {

	interface PreorderStoreModuleUiBinder extends UiBinder<Widget, PreorderStoreModule> {
	}

	private static PreorderStoreModuleUiBinder uiBinder = GWT.create(PreorderStoreModuleUiBinder.class);

	@UiField
	protected TextBox memo;
	@UiField
	protected FlowPanel skulistpanel, storepanel;
	private String storeId;
	@UiField
	protected Image storelogo;

	@UiField
	protected HTML storenamehtml;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月6日 下午10:28:14
	 */
	public PreorderStoreModule() {
		initWidget(uiBinder.createAndBindUi(this));

		memo.getElement().setAttribute("placeHolder", "给商家留言，选填，最多100个字");
		memo.setMaxLength(100);
	}

	@Override
	public Map<String, Object> getData() {
		Map<String, Object> storeSkuMap = new HashMap<String, Object>();
		storeSkuMap.put("STORE_ID", this.storeId);
		String memoStr = memo.getValue().trim();
		if (!StringUtils.isEmpty(memoStr))
			storeSkuMap.put("MEMO", memoStr);

		List<Map<String, Object>> skuList = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < skulistpanel.getWidgetCount(); i++) {
			Widget widget = skulistpanel.getWidget(i);
			if (widget instanceof IUIModule) {
				IUIModule uiModule = (IUIModule) widget;
				skuList.add(uiModule.getData());
			}
		}
		storeSkuMap.put("SKU_LIST", skuList);

		return storeSkuMap;
	}

	@Override
	public void setData(Map<String, Object> map) {
		this.storeId = String.valueOf(map.get("STORE_ID"));
		storelogo.setUrl(String.valueOf(map.get("STORE_LOGO")));
		storenamehtml.setText(String.valueOf(map.get("STORE_NAME")));

		skulistpanel.clear();
		List<Map<String, Object>> skuList = XJsonUtil.getList((JSONArray) map.get("SKU_LIST"));
		for (Map<String, Object> sku : skuList) {
			PreorderSkuModule preorderSkuModule = new PreorderSkuModule();
			preorderSkuModule.setData(sku);
			skulistpanel.add(preorderSkuModule);
		}
	}

}
